<template>
	<view class="home">
		<view class="search-box">
			<image src="/static/image/image/index-003.png" mode=""></image>
			<input type="text" value="" placeholder="请输入关键词搜索" />
		</view>

		<view class="banner" v-if="banner">
			<swiper class="swiper" autoplay indicator-dots indicator-color="rgba(255, 255, 255, 0.3)"
				indicator-active-color="#FFFFFF">
				<swiper-item class="swiper-item" v-for="(item, index) in banner">
					<image class="banner-img" :src="item.image" mode=""></image>
				</swiper-item>
			</swiper>
		</view>

		<view class="nav">
			<view class="nav-item" @click="toCompound()">
				<image src="/static/image/index-007.png" mode=""></image>
				<text>合成</text>
			</view>
			<view class="nav-item" @click="toNoticeList()">
				<image src="/static/image/index-008.png" mode=""></image>
				<text>公告</text>
			</view>
			<view class="nav-item" @click="toDeving('积分商城')">
				<image src="/static/image/index-009.png" mode=""></image>
				<text>积分商城</text>
			</view>
			<view class="nav-item" @click="toDeving('联系我们')">
				<image src="/static/image/index-010.png" mode=""></image>
				<text>联系我们</text>
			</view>
		</view>

		<view style="margin: 0 32rpx;">
			<scroll-view class="scroll-view_H" scroll-x="true">
				<block style="margin:0 32rpx;">
					<view class="jingang scroll-view-item_H">
						<view class="more-item" @click="toNoticeList()">
							<image src="/static/image/index-006.jpg" mode=""></image>
							<text>「平台公告」</text>
						</view>
					</view>
					<!-- 	<view class="jingang scroll-view-item_H">
						<view class="more-item" @click="toDeving('白名单公告')">
							<image src="/static/image/index-005.png" mode=""></image>
							<text>「白名单公告」</text>
						</view>
					</view> -->
					<view class="jingang scroll-view-item_H">
						<view class="more-item" @click="toactive">
							<image src="/static/image/index-005.jpg" mode=""></image>
							<text>「幸运抽奖」</text>
						</view>
					</view>
					<view class="jingang scroll-view-item_H">
						<view class="more-item" @click="toClub">
							<image src="/static/image/index-004.jpg" mode=""></image>
							<text>「元宇宙」</text>
						</view>
					</view>
				</block>
			</scroll-view>

		</view>

		<view class="list">
			<view class="list-tabs">
				<button :class="{active: index == topIndex}" v-for="(item, index) in tabs" :key="index"
					@click="changeTop(index)">
					{{ item }}
				</button>
			</view>
			<template v-if="topIndex == 0">
				<empty v-if="collectList.length == 0"></empty>
				<view class="list-content" v-else>
					<view class="list-item" v-for="(item, index) in collectList" :key="index"
						@click="goDetails(item.id)">
						<view class="item-image-box">
							<image :src="item.image" mode=""></image>
							<view class="item-num">
								<image src="/static/image/index-001.png" mode=""></image>
								<text>
									限量{{ item.stock }}份
								</text>
							</view>

							<view class="item-tag">
								<view v-if="item.status == 1" class="itemTips plr-20 ptb-8" style="
								    background: linear-gradient(
								      to right,
								      rgb(248, 144, 95),
								      rgb(254, 182, 80)
								    );
								  ">
									{{ item.status_text }}
								</view>

								<view class="itemTips plr-20 ptb-8" v-if="item.status == 2"
									style="color: #e66633; background: #00000090">
									{{ item.status_text }}
								</view>

								<view class="flex flex-start itemTips plr-20 ptb-8 bg-00" v-if="item.status == 3">
									<image src="../../static/time.png" class="timeImg mr-8"
										style="position: relative; z-index: 1;"></image>
									<view class="gold">
										{{ item.status_text }}
									</view>
								</view>

								<view class="itemTips plr-20 ptb-8 flex center" v-if="item.status == 4"
									style="background: linear-gradient(to right, #cf58e5, #fc76d5)">
									<view class="">
										{{ item.status_text }}
									</view>
									<u-count-down :timestamp="item.diff_time" bg-color="background:transparent"
										color="#fff" font-size="24" separator-color="#fff" style="margin-left: 5rpx"
										@end="endTime"></u-count-down>
								</view>
							</view>
						</view>
						<view class="item-name">{{ item.name }}</view>
						<view class="item-price">￥<text>{{ item.price }} </text></view>
					</view>
				</view>
			</template>

			<template v-if="topIndex == 1">
				<empty v-if="saleList.length == 0"></empty>
				<view class="list-content">
					<view class="list-item" v-for="(item, index) in saleList" :key="index"
						@click="goBoxDetails(item.id)">
						<view class="item-image-box">
							<image :src="item.image" mode=""></image>
							<view class="item-num">
								<image src="/static/image/index-001.png" mode=""></image>
								<text>
									限量{{ item.stock }}份
								</text>
							</view>
						</view>
						<view class="item-name">{{ item.name }}</view>
						<view class="item-price">￥<text>{{ item.price }}</text></view>
					</view>
				</view>
			</template>
		</view>


		<!-- 实名认证 -->
		<view v-if="authShow" class="white flex flex-between radius-20 size-26 ptb-30 pl-40 pr-22 authWrap">
			<view class=""> 完成实名认证，抢购快人一步 </view>
			<view class="ptb-8 plr-28 authMid bg-gold color-1a radius-35" @click="toAuth"
				style="margin-left: auto;margin-right: 20rpx;">
				实名认证
			</view>
			<u-icon name="close" color="#9A9A9A" @click="closeAuth"></u-icon>
		</view>
		<!-- 去登录 -->
		<view v-if="loginShow" class="white flex flex-between radius-20 size-26 ptb-30 pl-40 pr-22 authWrap">
			<view @click="toLogin"> 去登录，抢购快人一步 </view>
			<view class="flex flex-between">
				<view class="ptb-8 plr-20 authMid bg-gold color-1a radius-35 mr-20" @click="toLogin">
					登录
				</view>
				<u-icon name="close" color="#9A9A9A" @click="loginShow = false"></u-icon>
			</view>
		</view>

		<tabbar ref="tabbar"></tabbar>
	</view>
</template>

<script>
	import empty from "@/components/empty.vue";
	import tabbar from "@/components/tabbar.vue";
	export default {
		components: {
			empty,
			tabbar,
		},
		data() {
			return {
				flag: false, //上拉加载
				loginShow: false,
				token: "",
				page: 1,
				status: "more",
				authShow: false,
				topIndex: 0,
				tabs: ["藏品", "盲盒"],
				banner: [],
				collectList: [],
				saleList: [],
				conList: [],
				current: 0, //轮播图下标
				noticeList: [], //首页公告
				infoImage: {}, //背景图
				userInfo: {}
			};
		},
		onShow() {},
		onLoad() {
			this.token = uni.getStorageSync("token");
			if (this.token) {
				this.loginShow = false;
				this.$http("api/user/userInfo").then((res) => {
					if (res.is_auth == 0) {
						this.authShow = true;
					} else {
						this.authShow = false;
					}
				});
			} else {
				this.loginShow = true;
			}
			// 轮播图
			this.$http("api/index/bannerList").then((res) => {
				this.banner = res;
			});
			this.init();
			this.getgongGao();
			if (uni.getStorageSync('token')) {
				this.getUserInfo();
			}
			this.getImageBei();
		},
		onHide() {
			// this.collectList = [];
			// this.flag = false;
			// this.status = "more";
			// this.page = 1;
		},
		onReachBottom() {
			if (!this.flag) {
				this.status = "loading";
				this.page++;
				this.init();
			}
		},
		onPullDownRefresh() {
			this.flag = false;
			this.page = 1;
			this.collectList = [];
			this.init();
		},
		methods: {
			toactive() {
				// this.$u.toast('敬请期待！');
				uni.navigateTo({
					url: '/pages/drawLots/drawLots'
				})
			},
			toChat() {
				// const chatLink = 'https://192.168.0.137:9999/#/';
				const chatLink = 'https://zzyll.com.cn/h5/#/';
				const token = uni.getStorageSync('token');
				if (token) {
					location.href = `${chatLink}?token=${token}`;
				} else {
					uni.navigateTo({
						url: '/pages/login/index'
					});
				}
			},
			// 合成藏品
			toCompound() {
				uni.navigateTo({
					url: "/pages/chip/index",
				});
			},
			toDeving(title = '') {
				if (title == '联系我们') {
					uni.navigateTo({
						url: '/pages/my/callOur',
					});
				} else {
					uni.navigateTo({
						url: '/pages/deving?title=' + title,
					});
				}
			},
			getUserInfo() {
				this.$http("api/user/userInfo")
					.then((res) => {
						this.userInfo = res;
					})
					.catch((err) => {
						console.log(err);
					});
			},
			nextStep() {
				this.goDetails(this.collectList[0].id);
			},
			tiaoguo() {
				this.isShow = false;
				uni.setStorageSync("isLogin", 1);
			},
			getImageBei() {
				this.$http("api/newapi/imageBei").then((res) => {
					this.infoImage = res;
				});
			},
			//公告列表
			toNoticeList() {
				uni.navigateTo({
					url: "/pages/home/notice",
				});
			},
			//公告详情
			toNotice(id) {
				uni.navigateTo({
					url: `/pages/home/noticeDetails?id=${id}`,
				});
			},
			//首页公告
			getgongGao() {
				this.$http("api/Newapi/gongGao").then((res) => {
					this.noticeList = res.slice(0, 5);
				});
			},
			//去俱乐部
			toMetaverse() {
				uni.navigateTo({
					url: "/pages/home/club",
				});
			},
			//元宇宙互联
			toClub() {
				uni.navigateTo({
					url: "/pages/home/metaverse",
				});
			},
			toLogin() {
				uni.navigateTo({
					url: "../login/index",
				});
			},
			goCon(id) {
				uni.navigateTo({
					url: "./conDetails?id=" + id,
				});
			},
			toAuth() {
				uni.navigateTo({
					url: "../my/realAuth",
				});
			},
			toInvite() {
				uni.navigateTo({
					url: "/pages/my/myInvite?uuid=" + this.userInfo.uuid,
				});
			},
			toinvites() {
				uni.navigateTo({
					url: "/pages/my/invite",
				});
			},
			init() {
				if (this.topIndex == 0) {
					this.$http("api/goods/goodsList", {
						page: this.page,
						pagesize: 10,
					}).then((res) => {
						uni.stopPullDownRefresh();
						let data = res.data;
						if (data.length == 0) {
							this.flag = true;
							this.status = "noMore";
						} else {
							this.collectList = this.collectList.concat(data);
							if (data.length < 10) {
								this.flag = true;
								this.status = "noMore";
							}
						}
						// console.log("当前页码：：",this.page);
						// let data = res.data;
						// if (this.collectList >= res.count) {
						// 	this.status = "nomore"
						// } else {
						// 	this.page = this.page + 1;
						// 	this.status = "loadmore";
						// 	console.log("改变页码：：",this.page);
						// }
						// this.collectList = this.collectList.concat(data);
						// uni.stopPullDownRefresh();
					});
				} else if (this.topIndex == 1) {
					this.$http("api/box/boxList", {
						time_order: '',
						price_order: '',
						search: '',
						page: this.page,
						pagesize: 10,
					}).then((res) => {
						uni.stopPullDownRefresh();
						let data = res.data;
						if (data.length == 0) {
							this.flag = true;
							this.status = "noMore";
						} else {
							this.saleList = this.saleList.concat(data);
							if (data.length < 10) {
								this.flag = true;
								this.status = "noMore";
							}
						}
					});
				} else if (this.topIndex == 2) {
					this.$http("api/index/noticeList").then((res) => {
						this.conList = res;
					});
				}
			},
			toUrl(index) {
				uni.navigateTo({
					url: "./bannerCon?info=" + this.banner[index].content,
				});
			},
			changeTop(index) {
				this.topIndex = index;
				this.saleList = [];
				this.conList = [];
				this.page = 1;
				this.collectList = [];
				this.init();
			},
			goDetails(id) {
				uni.navigateTo({
					url: "./homeDetails?id=" + id,
				});
			},
			goBoxDetails(id) {
				uni.navigateTo({
					url: './boxDetails?id=' + id,
				});
			},
			closeAuth() {
				this.authShow = false;
			},
			changeDot(e) {
				this.current = e;
			},
			endTime() {
				this.saleList = [];
				this.conList = [];
				this.page = 1;
				this.collectList = [];
				this.init();
			},
		},
	};
</script>
<style lang="scss" scoped>
	.more-item {
		display: flex;
		flex-direction: column;
		align-items: center;

		image {
			width: 220rpx;
			height: 124rpx;
		}

		text {
			margin-top: 15rpx;
			font-size: 24rpx;
			color: #FFFFFF;
			opacity: 0.59;
		}
	}

	.scroll-view_H {
		// padding: 0 32rpx;
		white-space: nowrap;
		width: 100%;
	}

	.scroll-view-item {
		height: 300rpx;
		line-height: 300rpx;
		text-align: center;
		font-size: 36rpx;
	}

	.scroll-view-item_H {
		display: inline-block;
		// width: 334rpx;
		height: 180rpx;
		margin-right: 30rpx;
		position: relative;
	}

	.home {
		width: 100%;
		min-height: 100vh;
		overflow: hidden;
		box-sizing: border-box;
		background: #1E1F28;
	}

	.search-box {
		height: 72rpx;
		margin: 20rpx 40rpx;
		background: #25253B;
		border: 2rpx solid #303054;
		border-radius: 36rpx;
		display: flex;
		align-items: center;
		padding: 0 20rpx;

		image {
			width: 30rpx;
			height: 30rpx;
		}

		input {
			flex: 1;
			font-size: 26rpx;
			color: #FFFFFF;
			padding-left: 10rpx;

			&::placeholder {
				color: #A1A4B7;
			}
		}
	}

	.banner {
		margin: 30rpx 30rpx 0;

		.swiper {
			height: 330rpx;

			.swiper-item {
				height: 330rpx;
			}

			.banner-img {
				width: 100%;
				height: 330rpx;
				border-radius: 20rpx;
			}
		}
	}

	.nav {
		margin: 37rpx 50rpx;
		display: flex;
		justify-content: space-between;

		.nav-item {
			display: flex;
			flex-direction: column;
			align-items: center;

			image {
				width: 90rpx;
				height: 90rpx;
			}

			text {
				margin-top: 10rpx;
				line-height: 24rpx;
				font-size: 24rpx;
				color: #FFFFFF;
			}
		}
	}

	// .more {
	// 	margin: 0 25rpx;
	// 	display: flex;
	// 	justify-content: space-between;

	// 	.more-item {
	// 		display: flex;
	// 		flex-direction: column;
	// 		align-items: center;

	// 		image {
	// 			width: 220rpx;
	// 			height: 124rpx;
	// 		}

	// 		text {
	// 			margin-top: 15rpx;
	// 			font-size: 24rpx;
	// 			color: #FFFFFF;
	// 			opacity: 0.59;
	// 		}
	// 	}
	// }

	.list {
		margin-top: 50rpx;
		background: #0F0F0F;
		border-radius: 30rpx 30rpx 0 0;
		padding: 0 30rpx 80rpx;

		.list-tabs {
			display: flex;

			button {
				height: 100rpx;
				line-height: 100rpx;
				margin: 0;
				padding: 0;
				border: none;
				background: none;
				font-size: 28rpx;
				color: #FFFFFF;
				padding: 0 14rpx;
				margin-right: 50rpx;
				position: relative;

				&::after {
					display: none;
				}

				&.active::after {
					content: '';
					display: block;
					width: 32rpx;
					height: 4rpx;
					background: #A1A4B7;
					position: absolute;
					top: unset;
					bottom: 20rpx;
					left: calc(50% - 16rpx);
					transform: scale(1);
					border: none;
				}
			}
		}

		.list-content {
			display: flex;
			flex-wrap: wrap;

			.list-item {
				width: 334rpx;
				height: 454rpx;
				background: #222433;
				border-radius: 16rpx;
				overflow: hidden;
				margin-right: 20rpx;
				margin-bottom: 20rpx;

				&:nth-child(2n) {
					margin-right: 0;
				}

				.item-image-box {
					width: 100%;
					height: 334rpx;
					position: relative;
					border-radius: 16rpx;
					overflow: hidden;

					&>image {
						width: 334rpx;
						height: 334rpx;
					}

					.item-num {
						position: absolute;
						bottom: 0;
						left: 0;
						width: 100%;
						height: 50rpx;
						background: rgba(0, 0, 0, .6);
						display: flex;
						align-items: center;
						padding: 0 20rpx;

						&>image {
							width: 18rpx;
							height: 18rpx;
							margin-right: 2rpx;
						}

						text {
							font-size: 20rpx;
							color: #FFFFFF;
						}
					}

					.item-tag {
						position: absolute;
						top: 10rpx;
						right: 10rpx;

						.itemTips {
							background: #00000080;
							border-radius: 10rpx;
							color: #fff;
							font-size: 28rpx;
							z-index: 100;
							padding: 10rpx;
							font-weight: 600;
							max-width: 500rpx;
							text-overflow: ellipsis;
							white-space: nowrap;
							overflow: hidden;
						}
					}
				}

				.item-name {
					margin-top: 28rpx;
					line-height: 24rpx;
					font-size: 24rpx;
					color: #A9A8B6;
					padding: 0 14rpx;
					overflow: hidden; //超出的文本隐藏
					display: -webkit-box;
					-webkit-line-clamp: 1; // 超出多少行
					-webkit-box-orient: vertical;
				}

				.item-price {
					margin-top: 20rpx;
					margin-bottom: 28rpx;
					line-height: 24rpx;
					font-size: 24rpx;
					font-weight: normal;
					color: #FFFFFF;
					padding: 0 14rpx;
				}
			}
		}
	}

	.authWrap {
		position: fixed;
		bottom: var(--window-bottom);
		left: 50%;
		transform: translateX(-50%);
		z-index: 100;
		background: #222433;
		width: 100vw;
		padding-right: 40rpx;
		padding-left: 40rpx;
		padding-top: 20rpx;
		padding-bottom: 20rpx;
		border-radius: 0;
	}
</style>
